<?php 

/*********************************************************************
*  Class : Navigator
*  Function : 
*  Author :  joharijumali
*  Description: Class for Creating System Navigation
**********************************************************************/

class Navigator{

    /**
     * sidebar function
     * Create sidebar
     * @return string
     * @author joharijumali
     **/

  public static function sidebar() {

      $Menu = Console_Header::navigationdata();

      $navValue = array();


      if(Auth::check()):

        $isAdmin = Console_Role::where('sur_id','=',Auth::user()->sur_id)->where('sur_admin','=',1)->first();

        array_push($navValue, array(Str::title('Laman Utama') , url('home/dashboard'), false, false, null, 'tasks'));

        array_push($navValue, array(Navigation::DIVIDER));

      endif;

      
      foreach($Menu as $floor => $packet){
        
        array_push($navValue, array(Navigation::HEADER, Str::upper($packet['header']) ));
        if(!empty($packet['parent'])){

          foreach ($packet['parent'] as $key => $action){
              
              $aclPath = explode('/', $action['path']);

            if(Auth::check()){
              $aclAccess = Console_Acl::where('sur_id','=',Auth::user()->sur_id)
                       ->where('sua_modul','=',$aclPath[0])
                       ->where('sua_controller','=',$aclPath[1])
                       ->where('sua_action','=',$aclPath[2])
                       ->first();

              if(!empty($aclAccess)){
                
                $isActive = (URI::is($action['path']))?true:false;

                // if(URI::segment(5)):
                //   $isActive = ($aclPath[0].'/'.$aclPath[1] == URI::segment(2).'/'.URI::segment(3))?true:false;
                // endif;
                
                array_push($navValue, array(Str::title($action['alias']) , url($action['path']), $isActive, false, null, 'circle'));
              }

            }
          }


          
        }
    
        array_push($navValue, array(Navigation::DIVIDER));
      }
    
    $length = count($navValue);

    for($i = 0; $i < $length;$i++){
      $thot = $navValue[$i];
      if($i+1 != $length) $that = $navValue[$i+1];

      if($thot[0] == '-HEADER-' && $that[0] == '---'){
        unset($navValue[$i]);
      }

      if($thot[0] == '---'  ){
        unset($navValue[$i]);
      }

    }


    $final = Navigation::lists(Navigation::links($navValue));

    return $final;

  }

    /**
     * breadcrumb function
     * Create breadcrumb
     * @return string
     * @author joharijumali
     **/

  public static function breadcrumb(){


    

    if( URI::segment(2) != 'home' && URI::segment(3) != 'dashboard'):

      $isAdmin = Console_Role::where('sur_id','=',Auth::user()->sur_id)->where('sur_admin','=',1)->first();

      if($isAdmin):
        $butternbread['Laman Utama'] = url('home/dashboard');
      else:
        $butternbread['Laman Utama'] = url('attendance/schedule/individual');
      endif;

      $currentPage = Console_Page::where('smp_modul','=',URI::segment(2))
                                  ->where('smp_controller','=',URI::segment(3))
                                  ->where('smp_action','=',URI::segment(4))
                                  ->first();

      // $previousPage = Console_Page::join('sys_navigation_page','sys_navigation_page.smp_id','=','sys_modul_page.smp_id')
      //                             ->where('smp_modul','=',URI::segment(2))
      //                             ->where('smp_controller','=',URI::segment(3))
      //                             ->where('sys_modul_page.smp_id','<>',$currentPage->smp_id)
      //                             ->first();

      //                           echo "<pre>";print_r($previousPage);
      // $butternbread[$previousPage->smp_action_alias] = url($previousPage->smp_modul.'/'.$previousPage->smp_controller.'/'.$previousPage->smp_action);


      array_push($butternbread,Str::title($currentPage->smp_action_alias));

    else:

      $butternbread = array('Laman Utama');

    endif;

    return Breadcrumb::create($butternbread);

  }

}



?>